function writeData(str) {
	var len = Math.floor(Math.random() * 10);
	var chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
	var maxPos = chars.length;
	var pwd = '';
	var newStr = '';
	for ( i = 0; i < len; i++) {
		pwd += chars.charAt(Math.floor(Math.random() * maxPos));
	}
	newStr = 'gg' + len + pwd + str;
	return newStr;
}

function readData(str) {
	if (str.substr(0, 2) == 'gg' && (!isNaN(str.substr(2, 1)))) {
		var newStr = str;
		newStr = str.substring(2);
		newStr = newStr.substring(parseInt(newStr.charAt(0)) + 1)
	} else {
		newStr = '';
	}
	return newStr;
}

//读写购物车

function writeDataBuyCar(str) {
	//获取本地手机号 索引
	var indexPhone = readData(localStorage.phone);
	//加密字符串
	var len = Math.floor(Math.random() * 10);
	var chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
	var maxPos = chars.length;
	var pwd = '';
	var newStr = '';
	for ( i = 0; i < len; i++) {
		pwd += chars.charAt(Math.floor(Math.random() * maxPos));
	}
	var jsonBuycarstr, strPwd;
	//判断购物车是不是空的 是不是存在
	if (localStorage.buyCar) {//购物车存在
		var localbuycar = localStorage.buyCar;
		//取的加密的本地购物车
		if (localbuycar.substring(0, 2) == "ng" && (!isNaN(localbuycar.substr(2, 1)))) {//根据加密前缀判断是就的购物车 还是新的
			jsonBuycarstr = localStorage.buyCar.substring(2);
			jsonBuycarstr = jsonBuycarstr.substring(parseInt(jsonBuycarstr.charAt(0)) + 1)//获得整体所有成员购车
			var jsonBuycar = JSON.parse(jsonBuycarstr)//转变成json对象
			jsonBuycar[indexPhone] = JSON.parse(str);
			//购物车中写入对象
			//写入新的购物车对象
			strPwd = JSON.stringify(jsonBuycar);
		} else {//旧购物车
			//alert("进来")
			jsonBuycarstr = readData(localStorage.buyCar);
			var jsons = {};
			jsons[indexPhone] = JSON.parse(jsonBuycarstr);
			strPwd = JSON.stringify(jsons)//加密写入购物车
		}
	} else {
		var jsons = {};
		jsons[indexPhone] = JSON.parse(str);
		strPwd = JSON.stringify(jsons)//加密写入购物车
	}
	newStr = 'ng' + len + pwd + strPwd;
	return newStr;
}

function readDataBuyCar(str) {
	var indexPhone = readData(localStorage.phone);
	//获取本地手机号 索引
	var newStr = str;
	if (str.substr(0, 2) == 'ng' && (!isNaN(str.substr(2, 1)))) {//新购物车解密
		newStr = str.substring(2);
		newStr = newStr.substring(parseInt(newStr.charAt(0)) + 1)//获得整体所有成员购车
		var readjsonbuycar = JSON.parse(newStr);
		if (!readjsonbuycar[indexPhone]) {
			newStr = '';
		} else {
			var strbuy = readjsonbuycar[indexPhone];
			newStr = JSON.stringify(strbuy);
		}
	} else if (str.substr(0, 2) == 'gg' && (!isNaN(str.substr(2, 1)))) {
		newStr = str.substring(2);
		newStr = newStr.substring(parseInt(newStr.charAt(0)) + 1)
	} else {
		newStr = '';
	}
	return newStr;
}

function writeDataIMessage(str) {
	//获取本地手机号 索引
	var indexPhone = readData(localStorage.phone);
	//加密字符串
	var len = Math.floor(Math.random() * 10);
	var chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
	var maxPos = chars.length;
	var pwd = '';
	var newStr = '';
	var strPwd;
	for ( i = 0; i < len; i++) {
		pwd += chars.charAt(Math.floor(Math.random() * maxPos));
	}
	if (localStorage.IMmessage) {
		var localmessage = localStorage.IMmessage;
		if (localmessage.substring(0, 2) == "ng" && (!isNaN(localmessage.substr(2, 1)))) {
			var jsonMessagestr = localmessage.substring(2);
			jsonMessagestr = jsonMessagestr.substring(parseInt(jsonMessagestr.charAt(0)) + 1)
			var jsonMessage = JSON.parse(jsonMessagestr);
			jsonMessage[indexPhone] = JSON.parse(str);
			strPwd = JSON.stringify(jsonMessage);
		} else {
			localStorage.IMmessage = '';
			var json = {};
			json[indexPhone] = JSON.parse(str);
			strPwd = JSON.stringify(json);
		}
	} else {
		var json = {};
		json[indexPhone] = JSON.parse(str);
		strPwd = JSON.stringify(json);
	}
	newStr = 'ng' + len + pwd + strPwd;
	return newStr;
}

function readDataIMessage(str) {
	var indexPhone = readData(localStorage.phone);
	//获取本地手机号 索引
	var newStr;
	if (str.substr(0, 2) == 'ng' && (!isNaN(str.substr(2, 1)))) {
		newStr = str.substring(2);
		newStr = newStr.substring(parseInt(newStr.charAt(0)) + 1)//获得整体所有成员购车
		var readjsonMessage = JSON.parse(newStr);
		if (!readjsonMessage[indexPhone]) {
			newStr = '';
		} else {
			var strmessage = readjsonMessage[indexPhone];
			newStr = JSON.stringify(strmessage);
		}
	} else if (str.substr(0, 2) == 'gg' && (!isNaN(str.substr(2, 1)))) {
		newStr = str.substring(2);
		newStr = newStr.substring(parseInt(newStr.charAt(0)) + 1)
	} else {
		newStr = '';
	}
	return newStr;
}

function writeDataHistory(str) {
	//获取本地手机号 索引
	var indexPhone = readData(localStorage.phone);
	//加密字符串
	var len = Math.floor(Math.random() * 10);
	var chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
	var maxPos = chars.length;
	var pwd = '';
	var newStr = '';
	var strPwd;
	for ( i = 0; i < len; i++) {
		pwd += chars.charAt(Math.floor(Math.random() * maxPos));
	}
	if (localStorage.localHistory) {
		var localmessage = localStorage.localHistory;
		if (localmessage.substring(0, 2) == "ng" && (!isNaN(localmessage.substr(2, 1)))) {
			var jsonMessagestr = localmessage.substring(2);
			jsonMessagestr = jsonMessagestr.substring(parseInt(jsonMessagestr.charAt(0)) + 1)
			var jsonMessage = JSON.parse(jsonMessagestr);
			jsonMessage[indexPhone] = JSON.parse(str);
			strPwd = JSON.stringify(jsonMessage);
		} else {
			localStorage.localHistory = '';
			var json = {};
			json[indexPhone] = JSON.parse(str);
			strPwd = JSON.stringify(json);
		}
	} else {
		var json = {};
		json[indexPhone] = JSON.parse(str);
		strPwd = JSON.stringify(json);
	}
	newStr = 'ng' + len + pwd + strPwd;
	return newStr;
}
function writeDataVoiceimg(str) {//Android
	//获取本地手机号 索引
	var indexPhone = readData(localStorage.phone);
	//加密字符串
	var len = Math.floor(Math.random() * 10);
	var chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
	var maxPos = chars.length;
	var pwd = '';
	var newStr = '';
	var strPwd;
	for ( i = 0; i < len; i++) {
		pwd += chars.charAt(Math.floor(Math.random() * maxPos));
	}
	if (localStorage.localVoiceimg) {
		var localmessage = localStorage.localVoiceimg;
		if (localmessage.substring(0, 2) == "ng" && (!isNaN(localmessage.substr(2, 1)))) {
			var jsonMessagestr = localmessage.substring(2);
			jsonMessagestr = jsonMessagestr.substring(parseInt(jsonMessagestr.charAt(0)) + 1)
			var jsonMessage = JSON.parse(jsonMessagestr);
			jsonMessage[indexPhone] = JSON.parse(str);
			strPwd = JSON.stringify(jsonMessage);
		} else {
			localStorage.localVoiceimg = '';
			var json = {};
			json[indexPhone] = JSON.parse(str);
			strPwd = JSON.stringify(json);
		}
	} else {
		var json = {};
		json[indexPhone] = JSON.parse(str);
		strPwd = JSON.stringify(json);
	}
	newStr = 'ng' + len + pwd + strPwd;
	return newStr;
}
function writeDataGroupmsgnum(str) {//Android
	//获取本地手机号 索引
	var indexPhone = readData(localStorage.phone);
	//加密字符串
	var len = Math.floor(Math.random() * 10);
	var chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
	var maxPos = chars.length;
	var pwd = '';
	var newStr = '';
	var strPwd;
	for ( i = 0; i < len; i++) {
		pwd += chars.charAt(Math.floor(Math.random() * maxPos));
	}
	if (localStorage.localGroupmsgnum) {
		var localmessage = localStorage.localGroupmsgnum;
		if (localmessage.substring(0, 2) == "ng" && (!isNaN(localmessage.substr(2, 1)))) {
			var jsonMessagestr = localmessage.substring(2);
			jsonMessagestr = jsonMessagestr.substring(parseInt(jsonMessagestr.charAt(0)) + 1)
			var jsonMessage = JSON.parse(jsonMessagestr);
			jsonMessage[indexPhone] = JSON.parse(str);
			strPwd = JSON.stringify(jsonMessage);
		} else {
			localStorage.localGroupmsgnum = '';
			var json = {};
			json[indexPhone] = JSON.parse(str);
			strPwd = JSON.stringify(json);
		}
	} else {
		var json = {};
		json[indexPhone] = JSON.parse(str);
		strPwd = JSON.stringify(json);
	}
	newStr = 'ng' + len + pwd + strPwd;
	return newStr;
}
